package com.taxis.service;

import java.util.List;
import org.hibernate.*;
import com.taxis.common.*;
import com.taxis.model.Taxista;

public class TaxistaDaoService {

	private List<Taxista> taxistas;
	private Taxista taxista;

	public TaxistaDaoService(){ }

	public void insertTaxista(Taxista t) throws Exception{
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try{
			tx = session.beginTransaction();
			session.save(t);
			tx.commit();
		}catch(RuntimeException re){
			if(t != null){
				tx.rollback();
			}
			throw re;
		}finally{
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Taxista> getAllTaxistas() {
		Session session = HibernateUtil.currentSession();
		try {
			Query q = session.getNamedQuery("getAllTaxistas");
			taxistas = (List<Taxista>)q.list();
			return taxistas;
		} catch(Exception e) {
			System.out.println("Error while fetching TAXISTAS");
			e.printStackTrace();
			return null;
		} finally {
			session.close();        	
		}
	}

	public Taxista getTaxista(Integer idTaxista) throws Exception {
		Session session = HibernateUtil.currentSession();
		try {
			return (Taxista)session.get(Taxista.class, idTaxista);
		} finally {
			session.close();
		}
	}
	
	public List<Taxista> getTaxistasUnidad(int idUnidad) {
		Session session = HibernateUtil.currentSession();
		try {
			Query q = session.getNamedQuery("findByUnidad");
			q.setInteger("unidad", idUnidad);
			taxistas = (List<Taxista>)q.list();
			return taxistas;
		} catch(Exception e) {
			System.out.println("Error while fetching TAXISTAS");
			e.printStackTrace();
			return null;
		} finally {
			session.close();        	
		}
	}
	
	public void updateTaxista(Taxista t) throws Exception {
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(t);
			tx.commit();
		} catch(RuntimeException re) {
			if(t != null)
				tx.rollback();
			throw re;
		} finally {
			session.close();
		}
	}

	public void deleteTaxista(Integer idTaxista) throws Exception {
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			taxista = (Taxista)session.load(Taxista.class, idTaxista);
			session.delete(taxista);
			tx.commit();
		} catch (RuntimeException e) {
			if(taxista != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Taxista> findByEstado(String estado) {
		Session session = HibernateUtil.currentSession();
		try {
			Query q = session.getNamedQuery("findByEstado");
			q.setString("estado", estado);
			taxistas = (List<Taxista>)q.list();
			return taxistas;
		} catch(Exception e) {
			System.out.println("Error while fetching TAXISTAS");
			e.printStackTrace();
			return null;
		} finally {
			session.close();        	
		}
	}
	
	@SuppressWarnings("unchecked")
	public Taxista findByReporteAndPeticion(Integer idreporte) {
		Session session = HibernateUtil.currentSession();
		try {
			SQLQuery query = session.createSQLQuery("SELECT * from taxista inner join reporte on reporte.id_taxista = taxista.id_taxista where reporte.id_reporte=:idreporte");
			query.addEntity(Taxista.class);
			query.setInteger("idreporte",idreporte);
			Taxista t = (Taxista)query.uniqueResult();
			return t;
		} catch(Exception e) {
			System.out.println("Error while fetching TAXISTAS");
			e.printStackTrace();
			return null;
		} finally {
			session.close();        	
		}
	}
	
}